<script setup lang="tsx">
import { reactive, ref, unref } from 'vue'
import { GetUserOnlineList, DelUserOnline } from '@/api/whitejwt'
import { useTable } from '@/hooks/web/useTable'
import { useI18n } from '@/hooks/web/useI18n'
import { Table, TableColumn } from '@/components/Table'
import { ElTag } from 'element-plus'
import { Search } from '@/components/Search'
import { FormSchema } from '@/components/Form'
import { ElMessageBox, ElMessage } from 'element-plus'
import { ContentWrap } from '@/components/ContentWrap'
import { BaseButton } from '@/components/Button'
const { t } = useI18n()

const { tableRegister, tableState, tableMethods } = useTable({
  fetchDataApi: async () => {
    const res = await GetUserOnlineList({
      page_num: unref(page_num),
      page_size: unref(page_size),
      ...unref(searchParams)
    })
    return {
      list: res.data.list || [],
      total: res.data.total
    }
  }
})

const { dataList, loading, total, page_size, page_num } = tableState
const { getList, refresh } = tableMethods

const tableColumns = reactive<TableColumn[]>([
  {
    field: 'index',
    label: t('menuuser.index'),
    type: 'index'
  },
  {
    field: 'user_name',
    label: t('role.roleName')
  },
  {
    field: 'token_id',
    label: t('useronline.tokenid')
  },
  {
    field: 'device_type',
    label: t('login_info.device_type')
  },
  {
    field: 'ipaddr',
    label: t('login_info.ip')
  },
  {
    field: 'useronline',
    label: t('login_info.useronline'),

    slots: {
      default: (data: any) => {
        const useronline = data.row.useronline
        return (
          <>
            <ElTag type={useronline ? 'success' : 'danger'}>
              {useronline ? t('login_info.online') : t('login_info.offline')}
            </ElTag>
          </>
        )
      }
    }
  },
  {
    field: 'login_location',
    label: t('login_info.login_location')
  },
  {
    field: 'browser',
    label: t('login_info.browser')
  },
  {
    field: 'os',
    label: t('login_info.os')
  },
  {
    field: 'msg',
    label: t('login_info.login_msg')
  },
  {
    field: 'action',
    label: t('menuuser.action'),
    width: 120,
    slots: {
      default: (data: any) => {
        const row = data.row
        return (
          <>
            <BaseButton type="success" onClick={() => quituser(row)}>
              {t('useronline.quituser')}
            </BaseButton>
          </>
        )
      }
    }
  }
])

const quituser = async (row: any) => {
  ElMessageBox.confirm(t('useronline.forcequituser'), t('common.delWarning'), {
    confirmButtonText: t('common.delOk'),
    cancelButtonText: t('common.delCancel'),
    type: 'warning'
  })
    .then(async () => {
      const res = await DelUserOnline({ jid: row.jid })
      if (res) {
        ElMessage.success(t('useronline.quitsuccess'))
      } else {
        ElMessage.error(t('useronline.quitfail'))
      }
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: t('common.delCancel')
      })
    })
}

const searchSchema = reactive<FormSchema[]>([
  {
    field: 'user_name',
    label: t('role.roleName'),
    component: 'Input'
  }
])

const searchParams = ref({})
const setSearchParams = (data: any) => {
  searchParams.value = data
  getList()
}
</script>

<template>
  <ContentWrap>
    <Search :schema="searchSchema" @search="setSearchParams" :showReset="false" />

    <Table
      v-model:current-page="page_num"
      v-model:page-size="page_size"
      showAction
      :columns="tableColumns"
      default-expand-all
      node-key="id"
      :data="dataList"
      :loading="loading"
      :pagination="{
        total
      }"
      @register="tableRegister"
      @refresh="refresh"
    />
  </ContentWrap>
</template>
